/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package main;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.KeyEventDispatcher;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.image.BufferStrategy;
import java.util.ArrayList;
import java.util.LinkedList;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.Timer;
import model.Enemy;
import model.Player;

/**
 *
 * @author Nicklas Skole
 */
public class Game extends JFrame implements ActionListener {
    
    public final static int HEIGHT = 800;
    public final static int WIDTH = 600;
    public Player player;
    public Timer timer;
    private KeyboardFocusManager manager;
    private BufferStrategy bufferStrategy;
    private ArrayList<Enemy> enemyList = new ArrayList();
private LinkedList<Stone> bombs;   

    public Game() {
        super("Castle Defender");
        setSize(HEIGHT, WIDTH);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setResizable(false);
        setVisible(true);
        final JButton jb = new JButton("START");
        jb.setBounds(HEIGHT / 2 - 50, WIDTH / 2 - 150, 100, 300);
        add(jb);
        jb.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                init();
                remove(jb);
            }
        });
        revalidate();
        repaint();
        
        
    }
    
    private void init() {
        manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
        manager.addKeyEventDispatcher(new MyDispatcher());
        Image img = loadImage("Player2.png");
        
        //  public Player(int x, int y, int dx, int dy, int size, int hitpoint, int speed) {
        player = new Player(Game.WIDTH / 2, 150, 4, 0, 40, 100, 5,img);
        
        createBufferStrategy(2);
        bufferStrategy = getBufferStrategy();
        
        timer = new Timer(10, this);
        timer.start();
    }
    
    @Override
    public void paint(Graphics g) {
        draw(g);
    }
    
    public void draw(Graphics g) {
        Graphics2D g2d = (Graphics2D) g;
        
        g2d.setColor(new Color(0, 205, 255));
        g2d.fillRect(0, 0, HEIGHT, WIDTH);
        
        g2d.setColor(new Color(60, 179, 24));
        g2d.fillRect(0, 450, HEIGHT, WIDTH);
        
        
        /* Status block */
       if (player != null) {
        g2d.setFont(new Font("Impact",Font.BOLD,15));
        g2d.drawString("Player health "+player.getHealth(), 200, 50);
        
        g2d.drawString("Player point "+player.getPoint(), 400, 50);
       }
        
        
        g2d.drawImage(loadImage("wall.png"), 200, 220, null);
        
        g2d.setColor(Color.RED);
        int[] xPoints = {210, 160, 260};
        int[] yPoints = {150, 220, 220};
        g2d.fillPolygon(xPoints, yPoints, xPoints.length);
        
        int[] xPoints2 = {590, 540, 640};
        int[] yPoints2 = {150, 220, 220};
        g2d.fillPolygon(xPoints2, yPoints2, xPoints2.length);

        // Player
//        g2d.fillOval(player.getCenterX(), player.getY(), player.getSize(), player.getSize());
        g2d.drawImage(player.getImg(), player.getX(), player.getY(), null);
        
        drawEnemy(g2d);
        
    }
    
    public void drawEnemy(Graphics2D g2d){
        for (int i = 0; i < enemyList.size(); i++) {
            if (!enemyList.get(i).isIsAlive()) {
                // Enemy enemy = new Enemy(i, i, i, i, i, i, i, null)
            }
        }
    }
    
    public void move() {
        player.move();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        move();
        if (bufferStrategy != null) {
            Graphics g = bufferStrategy.getDrawGraphics();
            paint(g);
            bufferStrategy.show();
            g.dispose();
        }
    }

    /*
     * *********************** KEYBOARD EVENTS *******************************
     */
    private class MyDispatcher implements KeyEventDispatcher {
        // keyboard keys in this game

        @Override
        public boolean dispatchKeyEvent(KeyEvent e) {
            System.out.println("E:" + e);
            /* KEYPRESSED */
            if (e.getID() == KeyEvent.KEY_PRESSED) {
                int key = e.getKeyCode();
                
                if (key == KeyEvent.VK_LEFT) {
                    player.moveLeft();
                }
                
                if (key == KeyEvent.VK_RIGHT) {
                    player.moveRight();
                }
                
                if (key == KeyEvent.VK_SPACE) {
                   // player.dropstone();
                    
                }
                
            } /* KEY RELEASED */ else if (e.getID() == KeyEvent.KEY_RELEASED) {
                
                int key = e.getKeyCode();
                
                if (key == KeyEvent.VK_LEFT) {
                }
                
                if (key == KeyEvent.VK_RIGHT) {
                }
                
                if (key == KeyEvent.VK_SPACE) {
                }
                
            }
            return false;
        }
    }
    
    public Image loadImage(String filename) {
        ImageIcon icon;
        icon = new ImageIcon(this.getClass().getResource("/" + filename));
        return icon.getImage();
    }
}
